package com.dawn.module.member.controller.app.user;

import com.dawn.framework.common.enums.UserTypeEnum;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.security.core.annotations.PreAuthenticated;
import com.dawn.module.member.controller.app.user.vo.*;
import com.dawn.module.member.convert.user.MemberUserConvert;
import com.dawn.module.member.dal.dataobject.level.MemberLevelDO;
import com.dawn.module.member.dal.dataobject.user.MemberUserDO;
import com.dawn.module.member.service.level.MemberLevelService;
import com.dawn.module.member.service.user.MemberUserService;
import com.dawn.module.system.api.social.SocialUserApi;
import com.dawn.module.system.api.social.dto.SocialUserRespDTO;
import com.dawn.module.system.enums.social.SocialTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.dawn.framework.common.pojo.CommonResult.success;
import static com.dawn.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户个人中心")
@RestController
@RequestMapping("/member/user")
@Validated
@Slf4j
public class AppMemberUserController {

    @Resource
    private MemberUserService userService;

//    @Resource
//    private MemberLevelService levelService;

    @Resource
    private SocialUserApi socialUserApi;

    @GetMapping("/get")
    @Operation(summary = "获得基本信息")
//    @PreAuthenticated
    public CommonResult<AppMemberUserInfoRespVO> getUserInfo(@RequestParam(value = "id", required = false) Long id) {
        Long userId = id;
        if(userId == null){
            userId = getLoginUserId();
        }
        MemberUserDO user = userService.getUser(userId);
        AppMemberUserInfoRespVO appMemberUserInfoRespVO = MemberUserConvert.INSTANCE.convert(user);
//        MemberLevelDO level = levelService.getLevel(user.getLevelId());
//        SocialUserRespDTO socialUser = socialUserApi.getSocialUserByUserId(UserTypeEnum.MEMBER.getValue(),user.getId(), SocialTypeEnum.WECHAT_MP.getType());
//        if(socialUser != null){
//            appMemberUserInfoRespVO.setOpenid(socialUser.getOpenid());
//        }
        return success(appMemberUserInfoRespVO);
    }

    @PostMapping("/update")
    @Operation(summary = "修改基本信息")
    @PreAuthenticated
    public CommonResult<Boolean> updateUser(@RequestBody @Valid AppMemberUserUpdateReqVO reqVO) {
        userService.updateUser(getLoginUserId(), reqVO);
        return success(true);
    }

    @PostMapping("/update-mobile")
    @Operation(summary = "修改用户手机")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserMobile(@RequestBody @Valid AppMemberUserUpdateMobileReqVO reqVO) {
        userService.updateUserMobile(getLoginUserId(), reqVO);
        return success(true);
    }

    @PostMapping("/update-mobile-by-weixin")
    @Operation(summary = "基于微信小程序的授权码，修改用户手机")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserMobileByWeixin(@RequestBody @Valid AppMemberUserUpdateMobileByWeixinReqVO reqVO) {
        userService.updateUserMobileByWeixin(getLoginUserId(), reqVO);
        return success(true);
    }

    @PostMapping("/update-password")
    @Operation(summary = "修改用户密码", description = "用户修改密码时使用")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserPassword(@RequestBody @Valid AppMemberUserUpdatePasswordReqVO reqVO) {
        userService.updateUserPassword(getLoginUserId(), reqVO);
        return success(true);
    }

    @PostMapping("/reset-password")
    @Operation(summary = "重置密码", description = "用户忘记密码时使用")
    public CommonResult<Boolean> resetUserPassword(@RequestBody @Valid AppMemberUserResetPasswordReqVO reqVO) {
        userService.resetUserPassword(reqVO);
        return success(true);
    }

}

